package com.aoxon.cargo.activity.fragment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import psnl.littlemouse.vpwid.BaseFragment;
import psnl.littlemouse.vpwid.TabInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Cloth;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.component.Banner;
import com.aoxon.cargo.component.n.WaterFallGoodsView;
import com.aoxon.cargo.database.DataService;
import com.aoxon.cargo.service.HotGoodsService;
import com.aoxon.cargo.service.MyService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class PurFragment extends BaseFragment {

	private WaterFallGoodsView wShowGoods;
	private MyService goodsService;
	private boolean isIndex = false;
	private List<Cloth> cloths = new ArrayList<Cloth>();
	private DataService dataService;

	public PurFragment(TabInfo tb, String myService, boolean isIndex) {
		super(tb);
		goodsService = MyServiceFactory.getInstance(myService);
		this.isIndex = isIndex;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		dataService = new DataService(getActivity());
		wShowGoods = new WaterFallGoodsView(getActivity(), cloths,
				getTimeBox(inflater), isIndex);
		wShowGoods.setToLoad(ToLoadHot);
		if (isIndex) {
			final Banner banner = new Banner(getActivity());
			wShowGoods.getRootLayout().addView(banner.getBanner(), 0);
		}
		wShowGoods.loadData();
		return wShowGoods.getView();
	}

	private View getTimeBox(LayoutInflater inflater) {
		View timeBox = inflater.inflate(R.layout.time_box, null);
		TextView tv_day = (TextView) timeBox.findViewById(R.id.tv_day);
		TextView tv_week = (TextView) timeBox.findViewById(R.id.tv_week);
		TextView tv_date = (TextView) timeBox.findViewById(R.id.tv_date);

		final Calendar c = Calendar.getInstance();
		tv_day.setText(String.valueOf(c.get(Calendar.DAY_OF_MONTH)));

		final int month = c.get(Calendar.MONTH) + 1;
		StringBuilder sb = new StringBuilder().append(c.get(Calendar.YEAR))
				.append("-");
		if (month >= 10)
			sb.append(month);
		else
			sb.append("0").append(month);
		tv_date.setText(sb.toString());

		final int week = c.get(Calendar.DAY_OF_WEEK) - 1;
		switch (week) {
		case 0:
			tv_week.setText("星期日");
			break;
		case 1:
			tv_week.setText("星期一");
			break;
		case 2:
			tv_week.setText("星期二");
			break;
		case 3:
			tv_week.setText("星期三");
			break;
		case 4:
			tv_week.setText("星期四");
			break;
		case 5:
			tv_week.setText("星期五");
			break;
		case 6:
			tv_week.setText("星期六");
			break;
		default:
			break;
		}
		return timeBox;
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		wShowGoods.cancleLoad = true;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		wShowGoods.cancleLoad = false;
//		wShowGoods.notifyDataChange();
	}

	WaterFallGoodsView.ToLoad ToLoadHot = new WaterFallGoodsView.ToLoad() {

		@Override
		public boolean doLoad() throws Exception {
			// TODO Auto-generated method stub
			try {
			GsonBean gsonBean = goodsService.execute(wShowGoods.startIndex,
					wShowGoods.length);
			
				if (CheckStateUtil.check(gsonBean)) {
					List<Cloth> newCloths = DataUtil.gson.fromJson(
							gsonBean.getStrJson(),
							new TypeToken<List<Cloth>>() {
							}.getType());
					// 存入数据库 --xuan
					for (int i = 0; i < newCloths.size(); i++) {
						dataService.insertCloth(newCloths.get(i), returnType());
					}
					if (!wShowGoods.cancleLoad) {
						cloths.addAll(newCloths);
						wShowGoods.startIndex = cloths.size();
					}
					return true;
				} else {
					return false;
				}
			} catch (Exception e) {
				Log.v("cloths", " get database's Clothes");
				if(wShowGoods.startIndex == 0){
					cloths.addAll(dataService.getClothes(
							0, 12,
							returnType()));
				}else{
					cloths.addAll(dataService.getClothes(
							wShowGoods.startIndex, 12,
							returnType()));
				}
				if(wShowGoods.startIndex == cloths.size()){
					return false;
				}else{
					wShowGoods.startIndex = cloths.size();
					return true;
				}
				
			}

		}

	};

	private int returnType() {
		int type = -1;
		if (HotGoodsService.class.isInstance(goodsService)) {
			type = MyStateUtil.HOME_PAGE_HOT;
		} else {
			type = MyStateUtil.HOME_PAGE_NEW;
		}
		return type;
	}
}
